////////////////////////////////////////////////////////////////////////////////
/// @file    uartx.c
/// @author  AE TEAM
/// @brief   Output received data.
////////////////////////////////////////////////////////////////////////////////
/// @attention
///
/// THE EXISTING FIRMWARE IS ONLY FOR REFERENCE, WHICH IS DESIGNED TO PROVIDE
/// CUSTOMERS WITH CODING INFORMATION ABOUT THEIR PRODUCTS SO THEY CAN SAVE
/// TIME. THEREFORE, MINDMOTION SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT OR
/// CONSEQUENTIAL DAMAGES ABOUT ANY CLAIMS ARISING OUT OF THE CONTENT OF SUCH
/// HARDWARE AND/OR THE USE OF THE CODING INFORMATION CONTAINED HEREIN IN
/// CONNECTION WITH PRODUCTS MADE BY CUSTOMERS.
///
/// <H2><CENTER>&COPY; COPYRIGHT MINDMOTION </CENTER></H2>
////////////////////////////////////////////////////////////////////////////////
// Define to prevent recursive inclusion
#define _UARTX_C_

// Files includes
#include "delay.h"
#include "uartx.h"


////////////////////////////////////////////////////////////////////////////////
/// @addtogroup MM32_Hardware_Abstract_Layer
/// @{

////////////////////////////////////////////////////////////////////////////////
/// @addtogroup UARTX
/// @{

////////////////////////////////////////////////////////////////////////////////
/// @addtogroup UARTX_Exported_Functions
/// @{


////////////////////////////////////////////////////////////////////////////////
/// @brief  Get UART index
/// @param  UARTx
/// @retval UART index value
////////////////////////////////////////////////////////////////////////////////
u8 Get_UART_Index(UART_TypeDef* UARTx)
{
    if      (UARTx == UART1)    return 0;
    else if (UARTx == UART2)    return 1;
    return 0;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief  Setup this IP clock
/// @note   This function should affected by chip version.
/// @param  UARTx : Select UART port.
/// @param  NewState : Enable or disable the IP clock.
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void CFG_UART_Clock(UART_TypeDef* UARTx, FunctionalState NewState)
{
    if      (UARTx == UART1) {
        //CFG_IpClock(emIPCLOCK_UART1, NewState);
        RCC_APB1PeriphClockCmd(RCC_APB1ENR_UART1, NewState);
    }
    else if (UARTx == UART2) {
        //CFG_IpClock(emIPCLOCK_UART2, NewState);
        RCC_APB1PeriphClockCmd(RCC_APB1ENR_UART2, NewState);
    }
}

////////////////////////////////////////////////////////////////////////////////
/// @brief  UART1 Pin Config
/// @param  None
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
s32 Uart1PinConfigWithParameter(void)

{
    s32 result = 0;
    //customer can change below config based Pin assign
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_StructInit(&GPIO_InitStructure);
    RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOA, ENABLE);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_1);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_1);
    //UART1_TX   GPIOA.12
    GPIO_StructInit(&GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    //UART1_RX    GPIOA.3
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    return result;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief  Serial port initialization configuration
/// @note
/// @param  bound: Baud rate
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void Uart1InitWithParameter(u32 bound)
{
    UART_InitTypeDef UART_InitStructure;

    UART_StructInit(&UART_InitStructure);
    CFG_UART_Clock(UART1, ENABLE);
    //set as 8-N-1 Customer can change below parameters
    UART_InitStructure.BaudRate       = bound;
    UART_InitStructure.WordLength     = UART_WordLength_8b;
    UART_InitStructure.StopBits       = UART_StopBits_1;
    UART_InitStructure.Parity         = UART_Parity_No;
    UART_InitStructure.Mode           = UART_Mode_Rx | UART_Mode_Tx;
    UART_InitStructure.HWFlowControl  = UART_HWFlowControl_None;

    UART_Init(UART1, &UART_InitStructure);
    UART_Cmd(UART1, ENABLE);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief  received data.
/// @note   None.
/// @param  UARTx: UART1/UART2
/// @retval temp :data.
////////////////////////////////////////////////////////////////////////////////
u8 InputByte(UART_TypeDef* UARTx)
{
    u8 temp;

    while(1) {
        if(UART_GetITStatus(UARTx, UART_IT_RXIEN)) {
            UART_ClearITPendingBit(UARTx, UART_IT_RXIEN);
            break;
        }
    }

    temp = (u8)UART_ReceiveData(UARTx);
    if(temp == 0xd) {
        return 0;
    }
    return temp;
}

////////////////////////////////////////////////////////////////////////////////
/// @brief  send byte.
/// @note   None.
/// @param  dat(A byte data).
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void OutputByte(UART_TypeDef* UARTx, u8 dat)
{
    UART_SendData(UARTx, dat);
    while(!UART_GetFlagStatus(UARTx, UART_FLAG_TXEPT));
}

////////////////////////////////////////////////////////////////////////////////
/// @brief  Output received data.
/// @note   None.
/// @param  UARTx: UART1/UART2
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void UART_RxTx_Transmit_Test(UART_TypeDef* UARTx)
{
    u8 temp;
    temp = InputByte(UARTx);
    if(temp != 0) {
        OutputByte(UARTx, temp);
    }
}

////////////////////////////////////////////////////////////////////////////////
/// @brief  Serial port initialization configuration
/// @note    It must be careful of the Chip Version.
/// @param  bound: Baud rate
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void UART1_Polling_Init(u32 bound)
{
    Uart1InitWithParameter(bound);
    Uart1PinConfigWithParameter();
}



/// @}


/// @}

/// @}


